<?php

namespace app\api\model\service;

use think\Model;
class MiniTemplate extends Model
{
    // 表名
    protected $name = 'service_mini_template';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    public static function getTemplateConfig($attr)
    {
        return json_decode(self::where(['state'=>1])->value($attr),true);
    }

    public static function spellParams($params)
    {
        //9个模板 type 1接单通知用户2用户端订单通知3用户售后服务通知4投诉处理通知用户5服务者订单通知6服务者售后服务通知7商户订单通知8商户售后服务通知9商户订单完成通知
        $templateConfig = self::getTemplateConfig($params['templateAttr']);
        if(!$templateConfig)
        {
            return false;
        }
        unset($templateConfig['template_id']);
        $valueList = array_values($templateConfig);
        $keyList = array_keys($templateConfig);
        $data = [];
        foreach ($valueList as $key=>$value)
        {
            $length = self::getStrCount($value);
            if(in_array($keyList[$key],['url','urlParams'])){
                continue;
            }elseif($keyList[$key] == 'time')
            {
                $data[$value]['value'] = date("Y-m-d H:i",$params[$keyList[$key]]);
            }elseif ($keyList[$key] == 'name') {
                $data[$value]['value'] = substr($params[$keyList[$key]],0,$length);
            }elseif($keyList[$key] == 'note'){
                $data[$value]['value'] = $params[$keyList[$key]]?substr($params[$keyList[$key]],0,$length):'有问题请联系客服';
            }else{
                $data[$value]['value'] = substr($params[$keyList[$key]],0,$length);
            }
        }
        return $data;
    }

    public static function getStrCount($strType)
    {
        switch ($strType)
        {
            case strpos($strType, 'ame'):
            case strpos($strType,'hing'):
                $count = 18;
                break;
            case strpos($strType, 'etter'):
            case strpos($strType, 'haracter string'):
            case strpos($strType,'umber'):
                $count = 30;
                break;
            case strpos($strType,'hrase'):
                $count = 15;
                break;

            default :
                $count = 15;
        }
        return $count;
    }



}
